package cn.com.cifi.mars.util;


import cn.com.cifi.mars.bean.bo.SysLogBO;
import cn.com.cifi.mars.constant.Constants;
import cn.com.cifi.mars.constant.LogEventEnum;
import cn.com.cifi.mars.constant.LogSrcEnum;
import cn.com.cifi.mars.constant.LogTypeEnum;
import cn.com.cifi.mars.service.SysLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: jyx
 * @date: 2020/8/7 19:43
 */
@Component
public class SysLogUtils {

    @Autowired
    private static SysLogService sysLogService;

    @Autowired
    public void setComponent(SysLogService sysLogService){
        SysLogUtils.sysLogService = sysLogService;
    }

    /**
     * 添加日志（前端接口日志）
     * @param title
     * @param content
     * @param logEvent
     */
    public static void addSysLog(LogEventEnum logEvent, String moddule, String title, String content,LogSrcEnum logSrcEnum, String alias){
        SysLogBO bo = new SysLogBO();

        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();

        //操作事件
        bo.setEvent(logEvent);
        //标题
        bo.setTitle(title);
        //内容
        bo.setContent(content);
        //模块
        bo.setModule(moddule);
        //来源
        bo.setSource(logSrcEnum);
        //路径
        bo.setUrl(WebUtils.getUrl(request));
        //ip地址
        bo.setIp(WebUtils.getIpAddr(request));
        sysLogService.add(bo, alias);
    }

    /**
     * 添加日志(系统任务日志)
     * @param title
     * @param content
     * @param logEvent
     */
    public static void addSysLog(LogEventEnum logEvent, String moddule, String title, String content){
        SysLogBO bo = new SysLogBO();

        String hostAddr = WebUtils.getLocalIP();

        //操作事件
        bo.setEvent(logEvent);
        //标题
        bo.setTitle(title);
        //内容
        bo.setContent(content);
        //模块
        bo.setModule(moddule);
        //来源
        bo.setSource(LogSrcEnum.SYS);
        bo.setType(LogTypeEnum.ONE.getKey());
        //ip地址
        bo.setIp(hostAddr);
        sysLogService.add(bo, Constants.SYS);
    }
}
